import csv
import time
data_dict = {}
sum_list = []
file = csv.reader(open('data.csv','r'))
for item in file:
    # data_dict.setdefault("NotExist", []).append("Replace")
    product_name = item[0]
    jijin_name = item[1]
    start_time = item[2]
    end_time = item[3]
    timeArraystart = time.strptime(start_time, "%Y/%m/%d")
    otherstart_time = time.strftime("%Y/%m/%d", timeArraystart)
    timeArrayend = time.strptime(end_time, "%Y/%m/%d")
    otherend_time = time.strftime("%Y/%m/%d", timeArrayend)

    data_dict.setdefault(jijin_name, []).append((otherstart_time,otherend_time,product_name))

for  keys,value in data_dict.items():
    # print(keys,value)

    sort_date = sorted(value,key=lambda x: (x[1]),reverse=True)

    max_date = sort_date[0][1]
    filter_data = [item for item in sort_date if item[1] == max_date]
    sort_date_end = sorted(filter_data, key=lambda x: (x[0]), reverse=False)[0]
    # print(sort_date_end)
    for info in value:
        # i = 0
        if sort_date_end[0]<=info[1] <=sort_date_end[1]:
            # i += 1
            sum_list.append(info[-1])
    sum_num = len(sum_list)
    print(keys,sum_num,sum_list)
    with open('reslut.csv','a',encoding='utf8',newline='') as f:
        writer = csv.writer(f)
        writer.writerow((keys,sum_num,sum_list))
    del sum_list[:]


